package com.woniuxy.huayoung.controller;

import com.woniuxy.huayoung.entity.FlightInfos;
import com.woniuxy.huayoung.entity.Order;
import com.woniuxy.huayoung.entity.User;
import com.woniuxy.huayoung.result.ResponseResult;
import com.woniuxy.huayoung.service.FlightInfoService;
import com.woniuxy.huayoung.service.OrderService;
import com.woniuxy.huayoung.service.UserService;
import com.woniuxy.huayoung.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * @author dushen
 * @className: FlightInfosController
 * @description: TODO 飞机票查询接口
 * @create 2021-07-19-11:21
 */
@RestController
@RequestMapping("/flight/info")
@Slf4j
public class FlightInfosController {
    @Resource
    private UserService userService;

    @Resource
    private FlightInfoService flightInfoService;

    @Resource
    private OrderService orderService;

    /**
     *@author dushen
     *根据上传参数查询机票
     **/
    @GetMapping("getAll")
    public ResponseResult<List<FlightInfos>> getFlightInfo(FlightInfos flightInfos) {
        log.debug("FlightInfosController:getFlightInfo"+flightInfos);
        ResponseResult<List<FlightInfos>> responseResult = new ResponseResult<>();
        responseResult.setCode(200);
        responseResult.setMessage("请求成功");
        responseResult.setData(flightInfoService.findAllFlightInfos(flightInfos));
        return responseResult ;
    }

    /**
     *@author dushen
     *飞机票下单接口
     **/
    @PostMapping("placeOrder")
    public ResponseResult<Object> placeOrder(@RequestBody() Order flightOrder, HttpServletRequest request) {
        log.debug("FlightInfosController:getFlightInfo"+flightOrder);

        flightOrder.setOrderno(UUID.randomUUID().toString().replaceAll("-",""));
        //得到token
        String token = request.getHeader("authorization");
        System.out.println(JwtUtil.getAccount(token));
        User user = userService.findByTelephone(JwtUtil.getAccount(token));
        flightOrder.setUid(user.getId());
        flightOrder.setGoodname("机票");
        flightOrder.setNum(1);
        flightOrder.setType(3);
        flightOrder.setStatus(0);
        orderService.insertFlightOrder(flightOrder);

        ResponseResult<Object> responseResult = new ResponseResult<>();
        responseResult.setCode(200);
        responseResult.setMessage("下单成功");
        responseResult.setData(null);
        return responseResult ;
    }



}
